﻿using Device.ApplicationLib.Commands;
using Device.ApplicationLib.Queries;
using Device.CommonLib.Constants;
using Device.CommonLib.Models;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace Device.WebApi.Controllers
{
    [ApiController]
    public class CategoryController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ICategoryQuery _categoryQuery;

        public CategoryController(IMediator mediator,
            ICategoryQuery categoryQuery)
        {
            _mediator = mediator;
            _categoryQuery = categoryQuery;
        }
    
        [HttpGet(ApiRoutes.Category.QueryCategories)]
        public async Task<IActionResult> Get([FromQuery] CategorySearchRequestDto model)
        {
            if (model.Page > 0 || model.Limit > 0)
            {
                var data = await _categoryQuery.QueryCategoriesByPageAsync(model);
                return Ok(data);
            }
            else
            {
                var data = await _categoryQuery.QueryCategoriesAsync(model);
                return Ok(data);
            }
        }

        [HttpPost(ApiRoutes.Category.CreateCategory)]
        public async Task<IActionResult> Post([FromBody] CreateCategoryCommand model)
        {
            var data = await _mediator.Send(model);
            return Ok(data);
        }
    }
}